package com.ruoyi.program.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.ruoyi.program.domain.AliToken;

// 该类已经在对应文件中，推测问题是类名大小写问题，Java 类名应遵循大驼峰命名法，且文件名需与类名一致。
// 此处将类名修改为与文件名一致的 AliTokenController
@RestController
public class AliTokenController {
  /**
   * 处理支付宝回调请求，接收四个参数
   * 
   * @param appAuthCode 授权码
   * @param state       状态参数
   * @param appId       应用 ID
   * @param source      来源
   * @return 处理结果
   */
  @GetMapping("/test/api/aaa")
  public String handleAlipayCallback(
      @RequestParam("app_auth_code") String appAuthCode,
      @RequestParam("state") String state,
      @RequestParam("app_id") String appId,
      @RequestParam("source") String source) {

    // 这里可以添加对参数的处理逻辑
    // 查询数据库appid
    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCPSEQAnlhpaPL5xJ6u7wJAP2RNGgwp4GLsJvdzYpM68eIwXWg0E30pUGiHVL+2nPKS3nj/sgxcvlhRnLvq4BiSeefgh8o/XjAvR6HGKGu1be9hT52TGqev83jedkisS2xMtA+Dl7WZvh+HFgsYuNEMjTK5EkhHnXqSuEsQ8ycmov18riw8vYrWZJxyroXGjYWQogM6uU0q+ccJuXshZsmn1EuOYMITlr2e3z6K5OYgPlHoBAhQn/MI/S7hO4DdbXajAyVnjJEOpOoglNT+NTe/wDyPB97W9HdziLq82cTMVzu6VeyUOqIRFwi2ZOcDtcTR7OcnoeInCWR1Z+ATB1wjAgMBAAECggEAfg+apOCr8LIBdHblFgv68wPS/3L3WLCu2A7Aj6ge2TGPS/YihA7Rg9PW45pQwh373fQWiQDhl4dwtP0s6E0Z0WWn+Qf/jRkVU8O2NV3CxfFfN0KQ5KgC+O9B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zi7A1CBAWqTX/+yESZoE=";
    String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgODgBa+3Wav1n6aR7CDF9HoX38eicGwaFvxTppW6GUOi/xYAENdkdZvYDExxkDZ6LKh0yls/MnER7qTmT8wCeNB+HwC+fJpttTNRQsXn4Xv9jMxUv4wNc6L+2AFSQDIfRJHaRUHjJ0yU79t/w2mXXT4T9uKZMSyrCnKSIJ7gwYwPwRckMBkWklrfQs3jDyxJQWQ7zDCyUvBSBojh4hxWIwUY2WTEvsf91SyuDWSO2mDljVOaSobN/sKi3wVTXvU419+B48XX58fa2yklkV4argDxlZnd0mtT7aMh1zIfsNTekhhRpOgVl4qrR37KiDPZwT0GYlcgIB1QH7atzQiAqwIDAQAB";
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId,
        privateKey, "json", "GBK", alipayPublicKey, "RSA2");
    AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
    request.setGrantType("authorization_code");
    request.setCode(appAuthCode);

    // 为解决 response 无法解析的问题，需要将 response 变量的作用域扩大
    // 原代码中 response 定义在 try 块内，在块外无法访问，因此将其定义移到 try 块外
    AlipaySystemOauthTokenResponse response = null;
    try {
      response = alipayClient.execute(request);
    } catch (com.alipay.api.AlipayApiException e) {
      e.printStackTrace();
      // 可以根据实际需求添加更多的异常处理逻辑
    }

    if (response != null && response.isSuccess()) {
      System.out.println( response);
      System.out.println("调用成功");

      // 写库
      AliToken token = new AliToken();
      token.setState(state);

      // 假设 AliTokenMapper 有 insert 方法用于插入数据，将 save 替换为 insert
      // aliTokenMapper.insert(token);

      System.out.println("调用成功");
    } else {
      System.out.println("调用失败");
      System.out.println("ssssssssss" + response);
    }

    System.out.println("app_auth_code: " + appAuthCode);
    System.out.println("state: " + state);
    System.out.println("app_id: " + appId);
    System.out.println("source: " + source);

    return "Callback received successfully";
  }

}
